package com.dhp.mq;

import com.dhp.constant.MQConstant;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class PreMq {
    //TODO 上线的时候将该配置写成spring配置类
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        try {
            Connection connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
//            //创建死信队列和死信交换机
//            channel.exchangeDeclare(MQConstant.DEAD_EXCHANGE_NAME , "direct" , true , false , null);
//            channel.queueDeclare(MQConstant.DEAD_QUEUE_NAME , true , false , false , null);
//            channel.queueBind(MQConstant.DEAD_QUEUE_NAME , MQConstant.DEAD_EXCHANGE_NAME , MQConstant.DEAD_ROUTING_KEY);
//
//            //设置绑定信息
//            Map<String , Object> map = new HashMap<>();
//            map.put("x-dead-letter-exchange" , MQConstant.DEAD_EXCHANGE_NAME);
//            map.put("x-dead-letter-routing-key" , MQConstant.DEAD_ROUTING_KEY);
//
//            channel.exchangeDeclare(MQConstant.EXCHANGE_NAME , "direct" , true , false , null);
//            channel.queueDeclare(MQConstant.QUEUE_NAME , true , false , false , map);
//            channel.queueBind(MQConstant.QUEUE_NAME , MQConstant.EXCHANGE_NAME , "routingKey");
//            AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
//                    .expiration("3000") // 设置过期时间为5秒
//                    .build();
//            String message = "Hello World!";
//            channel.basicPublish("", MQConstant.QUEUE_NAME, properties, message.getBytes());

//
//            channel.exchangeDeclare(MQConstant.COMMON_DEAD_EXCHANGE , "direct" , true , false , null);
//           channel.queueDeclare(MQConstant.COMMON_DEAD_QUEUE , true , false , false , null);
//           channel.queueBind(MQConstant.COMMON_DEAD_QUEUE , MQConstant.COMMON_DEAD_EXCHANGE , MQConstant.COMMON_DEAD_KEY);

            channel.queueDelete(MQConstant.COMMON_QUEUE_NAME);
            Map<String , Object> map = new HashMap<>();
            map.put("x-dead-letter-exchange" , MQConstant.COMMON_DEAD_EXCHANGE);
            map.put("x-dead-letter-routing-key" , MQConstant.COMMON_DEAD_KEY);
            map.put("x-message-ttl" , MQConstant.COMMON_EXPIRE);
            channel.queueDeclare("common_ai_chart" , true , false , false , map);
            channel.queueBind("common_ai_chart" , MQConstant.EXCHANGE_NAME , MQConstant.COMMON_ROUTING_KEY);
            System.out.println("成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
    }
}
